/*

*/

#include "ff.h"
#include "stdint.h"
#include "file_zzp.h"




#define FILE_SIZE_MAX	(131070u*8019)

FIL UsbFile;
DIR UsbDir;

uint16_t FileOrder = 1;
uint32_t FileSize;

uint8_t	*FileBuf;///[65536];

uint8_t USB_SD_Buf_Inner[USB_XFER_BUF_SIZE];

void SetFileName_zzp(void)
{
	uint16_t i;
	
	UsbFileName[31 + 0] = (FileOrder / 1000) + 0x30;
	i = FileOrder % 1000;
	UsbFileName[31 + 1] = (i / 100) + 0x30;
	i = FileOrder % 100;
	UsbFileName[31 + 2] = (i / 10) + 0x30;
	i = FileOrder % 10;
	UsbFileName[31 + 3] = i + 0x30;
	
	FileOrder++;
}

uint8_t CreateDir_zzp(const uint8_t *path)
{
	uint8_t res = 0;
	FRESULT FRes;
	
	FRes = f_opendir(&UsbDir, (const TCHAR *)path);
	if (FRes == FR_OK)
		res = 1;
	else if (FRes == FR_NO_PATH){
		
		FRes = f_mkdir((const TCHAR *)path);
		if (FRes != FR_OK)
			return 0;
		res = 1;
	}	
	return res;
}


uint8_t CreateFile_zzp(void)
{
	uint8_t res = 0;
	
	res = CreateDir_zzp((const uint8_t *)UsbTrNoName);
	if (res){
				
		res = CreateDir_zzp((const uint8_t *)UsbDirName);
			
		if (res){
	
			SetFileName_zzp();
			if (f_open(&UsbFile, (const TCHAR *)UsbFileName, FA_CREATE_NEW | FA_WRITE) == FR_OK)
				res = 1;
			else
				res = 0;
		}
	}
		
	return res;
}

uint8_t CopyBigFile(void)
{
	uint32_t i, size, cnt;
	FRESULT res;
	
	FileBuf = USB_SD_Buf_Inner;
	for (i = 0; i < 65536; i++){
		FileBuf[i] = i & 0xff;
	}
	FileSize = FILE_SIZE_MAX;
	size = 65536;
	while (FileSize){
		
		cnt = 0;
		if (FileSize < size)
			size = FileSize;
		res = f_write (&UsbFile, FileBuf, size, &cnt);  
		if ((cnt != size) || (res != FR_OK)){
				
			f_close(&UsbFile);
			return 0;
		}
		FileSize -= size;
	}
	f_close(&UsbFile);
	return 1;
}








